package com.captain.special.二叉树.简单;

import com.captain.leetcode.TreeNode;

/**
 * Des:
 *
 * @author XL
 * @Date 2022/5/9 9:29
 */
public class 二叉搜索树节点最小距离783 {
    int min;
    int pre;

    public int minDiffInBST(TreeNode root) {
        min = Integer.MAX_VALUE;
        pre = -1;
        recur(root);
        return min;
    }

    public void recur(TreeNode node) {
        if (node == null) {
            return;
        }
        recur(node.left);
        int val = node.val;
        if (pre == -1) {
            pre = val;
        } else {
            min = Math.min(node.val - pre, min);
            pre = node.val;
        }
        recur(node.right);
    }
}
